package com.cmict.data.infra.controller.admin.consult;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.common.pojo.CommonResult;
import static com.cmict.data.framework.common.pojo.CommonResult.success;

import com.cmict.data.framework.excel.core.util.ExcelUtils;

import com.cmict.data.framework.operatelog.core.annotations.OperateLog;
import static com.cmict.data.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cmict.data.infra.controller.admin.consult.vo.*;
import com.cmict.data.infra.dal.dataobject.consult.ConsultDO;
import com.cmict.data.infra.convert.consult.ConsultConvert;
import com.cmict.data.infra.service.consult.ConsultService;

@Api(tags = "管理后台 - 咨询管理")
@RestController
@RequestMapping("/infra/consult")
@Validated
public class ConsultController {

    @Resource
    private ConsultService consultService;

    @PostMapping("/create")
    @ApiOperation("创建咨询管理")
    @PreAuthorize("@ss.hasPermission('sys:consult:create')")
    public CommonResult<Long> createConsult(@Valid @RequestBody ConsultCreateReqVO createReqVO) {
        return success(consultService.createConsult(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新咨询管理")
    @PreAuthorize("@ss.hasPermission('sys:consult:update')")
    public CommonResult<Boolean> updateConsult(@Valid @RequestBody ConsultUpdateReqVO updateReqVO) {
        consultService.updateConsult(updateReqVO);
        return success(true);
    }

    @PutMapping("/release")
    @ApiOperation("发布")
    @PreAuthorize("@ss.hasPermission('sys:consult:update')")
    public CommonResult<Boolean> releaseConsult(@Valid @RequestBody ConsultUpdateReqVO updateReqVO) {
        consultService.updateConsult(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除咨询管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:consult:delete')")
    public CommonResult<Boolean> deleteConsult(@RequestParam("id") Long id) {
        consultService.deleteConsult(id);
        return success(true);
    }
    @DeleteMapping("/batchDelete")
    @ApiOperation("批量删除咨询管理")
    @PreAuthorize("@ss.hasPermission('sys:consult:delete')")
    public CommonResult<Boolean> batchDeleteConsult(@RequestBody Collection<Long> ids) {
        consultService.batchDelete(ids);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得咨询管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:consult:query')")
    public CommonResult<ConsultRespVO> getConsult(@RequestParam("id") Long id) {
        ConsultDO consult = consultService.getConsult(id);
        return success(ConsultConvert.INSTANCE.convert(consult));
    }

    @GetMapping("/list")
    @ApiOperation("获得咨询管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('sys:consult:query')")
    public CommonResult<List<ConsultRespVO>> getConsultList(@RequestParam("ids") Collection<Long> ids) {
        List<ConsultDO> list = consultService.getConsultList(ids);
        return success(ConsultConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/choose")
    @ApiOperation("获得咨询管理列表(下拉框)")
    @ApiImplicitParam(name = "issueTitle", value = "问答标题", required = true, example = "测试", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('sys:consult:query')")
    public CommonResult<List<ConsultRespVO>> selectListChoose(@RequestBody ConsultChooseVO consultChooseVO) {
        List<ConsultDO> list = consultService.selectListChoose(consultChooseVO.getIssueTitle(),consultChooseVO.getId());
        return success(ConsultConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得咨询管理分页")
    @PreAuthorize("@ss.hasPermission('sys:consult:query')")
    public CommonResult<PageResult<ConsultRespVO>> getConsultPage(@Valid ConsultPageReqVO pageVO) {
        PageResult<ConsultDO> pageResult = consultService.getConsultPage(pageVO);
        return success(ConsultConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出咨询管理 Excel")
    @PreAuthorize("@ss.hasPermission('sys:consult:export')")
    @OperateLog(type = EXPORT)
    public void exportConsultExcel(@Valid ConsultExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ConsultDO> list = consultService.getConsultList(exportReqVO);
        // 导出 Excel
        List<ConsultExcelVO> datas = ConsultConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "咨询管理.xls", "数据", ConsultExcelVO.class, datas);
    }

}
